package com.example.Sao.MyReceiver;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.example.Sao.MyActivity.PCActivity;
import com.example.Sao.MyBean.Event;
import com.example.Sao.Tools.LogUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.MyReceiver
 * @ClassName: USBReceiver
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/3/20 15:30
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/3/20 15:30
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ServiceReceiver extends BroadcastReceiver {
    private String TAG  = "USBReceiver";
     private String ACTION = "com.example.application";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ACTION)) {
                //是否进入开发者模式并且在栈中该activity不存在
                if (isdevelopmode(context)&&!isExistMainActivity(PCActivity.class,context)) {
                    Log.d(TAG,"开启服务");
                    Intent i= new Intent();
                    i.setClass(context,PCActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
    }

    /**
     * 是否进入开发者模式
     */
    public boolean isdevelopmode(Context context) {
        boolean enableAdb = (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) > 0);
        if (enableAdb) {
            LogUtil.d(TAG, "已进入开发者模式");
//            MyToast.setToast(getApplicationContext(),"已进入开发者模式");
        } else {
            Toast.makeText(context,"还未进入开发者模式，请先开启开发者模式",Toast.LENGTH_LONG).show();
            LogUtil.d(TAG, "还未进入开发者模式，请先开启开发者模式");
        }
        return enableAdb;
    }

    /**判断activity在栈里的启动情况
     * @param activity
     * @param context
     * @return
     */
    private boolean isExistMainActivity(Class<?> activity,Context context) {
        Intent intent = new Intent(context, activity);
        ComponentName cmpName = intent.resolveActivity(context.getPackageManager());
        boolean flag = false;
        if (cmpName != null) { // 说明系统中存在这个activity    
            ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> taskInfoList =am.getRunningTasks(10);
            ActivityManager.RunningTaskInfo runningTaskInfo = taskInfoList.get(0);// 只拿当前运行的栈
            int numActivities = taskInfoList.get(0).numActivities;
            for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                if (taskInfo.baseActivity.equals(cmpName)) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
}
